package com.ld.control.business.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.robot.mapper.RobotPointMapper;
import com.ld.control.business.robot.model.RobotPointModel;
import com.ld.control.business.robot.service.RobotPointService;
import com.ld.control.business.robot.vo.req.RobotPointReqVo;
import com.ld.control.business.robot.vo.resp.RobotPointRespVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 机械臂点位信息 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-07-08
 */
@Service
public class RobotPointServiceImpl extends ServiceImpl<RobotPointMapper, RobotPointModel> implements RobotPointService {

    @Autowired
    private RobotPointMapper robotPointMapper;

    @Override
    public void add(RobotPointReqVo reaVo) {
        addOrUpdateCheck(reaVo);
        RobotPointModel robotPointModel = new RobotPointModel();
        BeanUtils.copyProperties(reaVo, robotPointModel);
        robotPointMapper.insert(robotPointModel);
    }

    @Override
    public void update(RobotPointReqVo reaVo) {
        addOrUpdateCheck(reaVo);
        RobotPointModel robotPointModel = new RobotPointModel();
        BeanUtils.copyProperties(reaVo, robotPointModel);
        robotPointMapper.updateById(robotPointModel);
    }

    @Override
    public void delete(Long id) {
        robotPointMapper.deleteById(id);
    }

    @Override
    public List<RobotPointRespVo> getList(Long regionId) {
        IPage<RobotPointRespVo> iPage = new Page<>(1, 9999);
        IPage<RobotPointRespVo> iPage1 = robotPointMapper.selectListByCondition(iPage, null, null, null, null, null);
        return iPage1.getRecords();
    }

    @Override
    public GeneratePageListVo<RobotPointRespVo> getPageList(Long regionId, String name, Integer type, String targetPoint, String initPoint, Integer current, Integer size) {
        IPage<RobotPointRespVo> iPage = new Page<>(current, size);
        IPage<RobotPointRespVo> iPage1 = robotPointMapper.selectListByCondition(iPage, regionId, name, type, targetPoint, initPoint);
        return new GeneratePageListVo<>(iPage1);
    }

    @Override
    public RobotPointRespVo getById(Long id) {
        RobotPointRespVo respVo = new RobotPointRespVo();
        RobotPointModel robotPointModel = robotPointMapper.selectById(id);
        BeanUtils.copyProperties(robotPointModel, respVo);
        return respVo;
    }

    @Override
    public RobotPointRespVo getByName(String name) {
        RobotPointRespVo respVo = new RobotPointRespVo();
        QueryWrapper<RobotPointModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(RobotPointModel.NAME, name);
        RobotPointModel robotPointModel = robotPointMapper.selectOne(queryWrapper);
        BeanUtils.copyProperties(robotPointModel, respVo);
        return respVo;
    }

    /**
     * 添加或修改校验
     * @param reaVo
     */
    private void addOrUpdateCheck(RobotPointReqVo reaVo) {
        QueryWrapper<RobotPointModel> queryWrapper = new QueryWrapper<>();
        if (reaVo.getId() != null) {
            queryWrapper.ne(RobotPointModel.ID, reaVo.getId());
        }
        queryWrapper.eq(RobotPointModel.NAME, reaVo.getName());
        Long count = robotPointMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException("点位名称已经存在！");
        }

    }
}
